package com.zhao.controller;

import com.zhao.dao.PublicizeDao;
import com.zhao.dao.StudentDao;
import com.zhao.dao.SuperviseDao;
import com.zhao.dao.UniversityDao;
import com.zhao.pojo.*;
import com.zhao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private SuperviseService superviseService;


    @Autowired
    private PublicizeService publicizeService;

    @Autowired
    private EbickGoodsService ebickGoodsService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private WalletService walletService;

    @RequestMapping("adminLogout.do")
    public String adminLogout(HttpSession session){

        session.removeAttribute("admin");

        session.removeAttribute("user");
        return "redirect:/adminLogin";
    }

    @RequestMapping("adminLogin.do")
    public String adminLogin(User user, HttpSession session){
        System.out.println("admin login");
        String res = loginService.adminLogin(user,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "redirect:/adminLogin";
        }

        Admin admin = (Admin) session.getAttribute("admin");
        //ebickGoodsService.getEbickGoodsNum(admin.getUniversityID(),session);
        res = superviseService.getSupervisesNum(admin.getUniversityID(),session);

        //studentService.getStudentNum(admin.getUniversityID(),session);
        ebickService.adminGetAllEbickNum(admin,session);
        orderFormService.adminGetFinishenOrdersNum(admin,session);

        return "redirect:/adminIndex";
    }

    @RequestMapping("logout.do")
    public String logout(HttpSession session){

        if(session.getAttribute("student")!=null){
            session.removeAttribute("student");
        }else if(session.getAttribute("schoolCounsellor")!=null){
            session.removeAttribute("schoolCounsellor");
        }else {
            session.removeAttribute("superviser");

        }

        session.removeAttribute("user");

        return "redirect:/";
    }

    @RequestMapping("login.do")
    public String managerLongin(User user, HttpSession session){
        System.out.println(user);
        //登录信息分发
        switch (user.getUserType()){
            case 1: //学生
                return stuLogin(user,session);
            case 2: //辅导员
                return teacherLogin(user,session);
            case 3: //监督员
                return superviserLogin(user,session);
            default:
                session.setAttribute("loginMessage","用户名或密码错误");
                return "redirect:/";
        }


    }

    @Autowired
    private UniversityDao universityDao;

    @Autowired
    private DrivingLicenseService drivingLicenseService;

    @Autowired
    private OrderFormService orderFormService;


    //学生登录
    private String stuLogin(User user,HttpSession session){

        University university = universityDao.findByName(user.getUniversityName());

        Student stu=loginService.findByStudentID(user.getUsername(),university.getUniversityID());
        System.out.println(stu);

        /////登录验证逻辑 要在 service中完成
        if(stu!=null && stu.getPassWord().equals(user.getPassword())){//登录验证通过
            user.setUserID(stu.getStudentID());
            user.setUsername(stu.getName());
            user.setUniversityID(stu.getUniversityID());
            session.setAttribute("user",user);
            session.setAttribute("student",stu);
            System.out.println(stu);
            System.out.println("验证是否完善信息");
            //验证用户是否没有完善信息，必须完善信息！
            /*System.out.println("0 前");
            if(stu.getIsRegisted().equals("0")){
                System.out.println("000");
                return "redirect:/getStudentInfo";
            }*/

            ////查看被举报数量
            System.out.println("查看被举报数量");
            int num = superviseService.getNumOFSuperviseMe(stu);
            session.setAttribute("numOFSuperviseMe",num);

            ///查询行驶证
            System.out.println("查看自己的行驶证明");
            String res = drivingLicenseService.getDrivingLicenseImpl(session);
            System.out.println(res);
            if(res.equals("没有行驶证")){
                session.setAttribute("getDrivingLicenseMessage","没有行驶证");
            }

            ///查询最新公告
            res = getNew3Publicize(session,stu.getUniversityID());
            System.out.println(res);
            if(!res.equals("success")){
                System.out.println("error");
            }
            //获取主页数据
            Student student = (Student) session.getAttribute("student");
            ebickGoodsService.getEbickGoodsNum(student.getUniversityID(),session);
            superviseService.getSupervisesNum(student,session);
            studentService.getStudentNum(student.getUniversityID(),session);
            //获取主页钱包

            walletService.getMyWallet(student,session);
            //获取商品信息
            ebickGoodsService.studentSeeAllEbickGoods(student,session);
            //获取别举报
            superviseService.getSuperviseMe(session);
            return "redirect:/studentIndex";
        }else{//验证失败
            session.setAttribute("loginMessage","用户名或密码错误");
            return "redirect:/login";
        }

    }

    @Autowired
    private EbickService ebickService;

    //辅导员登录
    private String teacherLogin(User user,HttpSession session){

        String res = loginService.schoolCounsellorLogin(user,session);
        System.out.println(res);
        session.setAttribute("loginMessage",res);
        if(res.equals("success")){

            SchoolCounsellor schoolCounsellor =(SchoolCounsellor)session.getAttribute("schoolCounsellor");

            res = superviseService.getSuperviseMyStudent(schoolCounsellor,session);
            System.out.println("res = "+res);
            if(res.equals("success")){
                List<SuperviseFormImpl> mySuperviseFormImpleList1 = (List<SuperviseFormImpl>)session.getAttribute("mySuperviseFormImpleList1");
                System.out.println("size "+mySuperviseFormImpleList1.size());
                session.setAttribute("needDealStudentSuperviseNum",mySuperviseFormImpleList1.size());
            }else {
                session.setAttribute("needDealStudentSuperviseNum",0);
            }
            schoolCounsellor =(SchoolCounsellor)session.getAttribute("schoolCounsellor");
            //获取主页数据
            //ebickGoodsService.getEbickGoodsNum(schoolCounsellor.getUniversityID(),session);
            superviseService.getSupervisesNum(schoolCounsellor,session);
            //studentService.getStudentNum(schoolCounsellor.getUniversityID(),session);
            orderFormService.schoolCounsellorGetFinishedOrderNum(schoolCounsellor,session);
            ebickService.getMyStudentEbickNum(schoolCounsellor,session);
            return "redirect:/toSchoolCounsellorIndex.do";
        }


        return "redirect:/";
    }

    //监督人员登录
    private String superviserLogin(User user,HttpSession session){

        String res = loginService.superviserLogin(user,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "redirect:/login";
        }

        return "redirect:/superviser-index";
    }


    private String getNew3Publicize(HttpSession session,String universityID){

        String res = publicizeService.getNew3Publicize(session,universityID);

        if(!res.equals("success")){
            return "404";
        }

        return "success";
    }

}
